package business.inhospitalqc;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import common.Data;
import common.RunCaseJson;
import common.Yaml;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import utils.GetFileMess;
import utils.YamlReadUtil;

import java.io.FileNotFoundException;

import static common.Login.login;
import static common.TestStep.requestAndRespondBody;

/*获取医生列表
 * */
public class DoctorList {
    Logger log = Logger.getLogger(DoctorList.class);
    public static final String system = "env.properties";
    Data data = login();

    /**
    * 
    * @Description: 获取医生列表信息
     * @param i	api-users/user/list的datas数据
    * @return java.lang.String
    * @author gujinle
    * @date 2021-08-23 18:00
    */
    public String GetUserList(int i) throws ConfigurationException, FileNotFoundException, YamlException {
        JSONObject json = new JSONObject();
//        try {
            String orgId = new GetFileMess().getValue("orgId", system);
            String path=new GetFileMess().getValue("ymlPath",system);
            Yaml yaml= YamlReadUtil.getLocatorMap(path,this.getClass().getName());
            json.put("access_token", data.getAccessToken());
            json.put("authToken", data.getAccessToken());
            json.put("uniqu_no", data.getUniquNo());
            json.put("operatorId", data.getUserId());
            json.put("authUserId", data.getUserId());
            json.put("keyword", data.getUserName());
            json.put("page", yaml.getDatas().get(i).get("page"));
            json.put("size", yaml.getDatas().get(i).get("size"));
            json.put("path", "400," + orgId + ",");    //机构path
            json.put("nroleId", yaml.getDatas().get(i).get("nroleId"));
//        } catch (ConfigurationException | FileNotFoundException | YamlException e) {
//            e.printStackTrace();
//        }
        log.info("请求参数：" + json.toString());
        Response response = RunCaseJson.runCase(json, "get", yaml.getUri());
        String jsonStr = response.asString();

        String url = RestAssured.baseURI + ":" + RestAssured.port + RestAssured.basePath +  yaml.getUri();
        requestAndRespondBody(url,json,jsonStr);   //写进allure报告

        log.info("响应报文：" + jsonStr);
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        JSONObject obj = jsonObject.getJSONObject("data");
        JSONArray jsonArray = obj.getJSONArray("content");
        JSONObject contentojb = jsonArray.getJSONObject(0);
        String practitionerId = contentojb.getString("id");  //返回医生id
        return practitionerId;

    }
}
